﻿using lBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using System.Reflection;

namespace WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CaiPuController : ControllerBase,lMemBerBll
    {
        public CaiPuController(lMemBerBll bll,ILogger<CaiPuController> logger,IWebHostEnvironment web)
        {
            Bll = bll;
            Logger = logger;
            Web = web;
        }

        public lMemBerBll Bll { get; }
        public ILogger<CaiPuController> Logger { get; }
        public IWebHostEnvironment Web { get; }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">定义名称</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public dynamic Img(IFormFile file)
        {
            try//异常处理
            {
                Logger.LogInformation("上传图片成功");
                string n = DateTime.Now.ToString("yyyyMMddhhmmss");
                string path = Web.WebRootPath + @"\img\" + n + file.FileName;
                using (FileStream fs = System.IO.File.Create(path))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return "/img/" + n + file.FileName;
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
        }



        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="certyeoy">表名</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> Add(Certyeoy certyeoy)
        {
            try//异常处理
            {
                Logger.LogInformation("添加成功");
                return Bll.Add(certyeoy);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
           
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="cname">食谱标题模糊查询</param>
        /// <param name="typeind">分类下拉精确查询</param>
        /// <returns></returns>
        [HttpGet]
        public dynamic Show(string? cname, int? typeind)
        {
            try//异常处理
            {
                Logger.LogInformation("显示成功");
                return Bll.Show(cname, typeind);
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }

        /// <summary>
        /// 下拉分类
        /// </summary>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public dynamic Xia()
        {
            try//异常处理
            {
                Logger.LogInformation("下拉框显示成功");
                return Bll.Xia();
            }
            catch (Exception)
            {
                Logger.LogError("报错异常");
                throw;
            }
            
        }
    }
}
